package com.ilarele.shopping.client.activities;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.os.Bundle;

import com.flurry.android.FlurryAgent;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.ilarele.shopping.client.R;
import com.ilarele.shopping.client.util.Global;

public class CustomShowMapView extends MapActivity {
	private static final int E16 = 1000000;
	private MapController mc;
	private GeoPoint markedPoint;
	private PinMapOverlay myPinOverlay;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map_show_layout);
		final MapView mapView = (MapView) findViewById(R.id.mapview);

		myPinOverlay = new PinMapOverlay();
		mapView.getOverlays().add(myPinOverlay);

		mapView.setBuiltInZoomControls(true);
		mapView.setSatellite(false);
		//
		mc = mapView.getController();
		mc.setZoom(18);

		mapView.setClickable(true);
		mapView.setEnabled(true);
		mapView.displayZoomControls(true);

		final MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this,
				mapView);
		mapView.getOverlays().add(myLocationOverlay);
		myLocationOverlay.enableCompass();

		Intent receiveIntent = getIntent();
		boolean hasLocation = receiveIntent.getBooleanExtra("hasLocation",
				false);
		if (hasLocation) {
			double longitude = receiveIntent.getDoubleExtra("longitude",
					Global.DEFAULT_LONGITUDE);
			double latitude = receiveIntent.getDoubleExtra("latitude",
					Global.DEFAULT_LATITUDE);
			markedPoint = new GeoPoint(getE6(latitude), getE6(longitude));
			mc.animateTo(markedPoint);
		} else {
			myLocationOverlay.enableMyLocation();
			myLocationOverlay.runOnFirstFix(new Runnable() {
				public void run() {
					markedPoint = myLocationOverlay.getMyLocation();
					mc.animateTo(markedPoint);
					System.out.println("runOnFirstFix " + markedPoint);
				}
			});
		}
	}

	private int getE6(double degree) {
		return (int) (degree * E16);
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	class PinMapOverlay extends com.google.android.maps.Overlay {
		@Override
		public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
				long when) {
			super.draw(canvas, mapView, shadow);
			if (markedPoint != null) {
				// translate the GeoPoint to screen pixels
				Point screenPts = new Point();
				mapView.getProjection().toPixels(markedPoint, screenPts);

				// add the marker
				Bitmap bmp = BitmapFactory.decodeResource(getResources(),
						R.drawable.pin);

				int width = bmp.getWidth();
				int height = bmp.getHeight();
				canvas.drawBitmap(bmp, screenPts.x - width / 2, screenPts.y
						- height / 2, null);
			}
			return true;
		}
	}

	@Override
	public void onStart() {
		super.onStart();
		if (!Global.DEBUG_MODE)
			FlurryAgent.onStartSession(this, Global.FLURRY_ID);
	}

	@Override
	public void onStop() {
		super.onStop();
		if (!Global.DEBUG_MODE)
			FlurryAgent.onEndSession(this);
	}
}